<?php
/**
 * 添加工单延期配置
 * User: Administrator
 * Date: 2018\7\3 0003
 * Time: 19:13
 */

namespace app\lib\Order\OrderExtensionConfig;


class AddOrderExtensionConfig extends OrderExtensionConfigBase
{
    /**
     * 参数验证
     * @return mixed
     * @throws \Exception
     */
    private function validate(){
        $model = $this->getTable();
        $data = $this->handleArray();
        $data[$model->_createId] = $this->opUid;
        $data[$model->_createTime] = time();
        $validate = $this->getValidate();
        if(!$validate->scene('add')->check($data)){
            exception($validate->getError());
        }
        $map = [$model->_title=>$data['title']];
        $id =  $model->where($map)->value($model->_id);
        if(!$id){
            exception('配置名称不能重复');
        }
        return $data;
    }

    /**
     * 执行添加配置
     * @return bool
     * @throws \Exception
     */
    public function save(){
        $data = $this->validate();
        $model = $this->getTable();
        $result =  $model->insert($data);
        if(!$result){
            exception('操作失败');
        }
        return true;
    }
}